package com.rpframework.website.device.act.api;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.GenerateUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.act.admin.AdminAct;
import com.rpframework.website.device.domain.Opinion;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.domain.Widens;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.OpinionService;
import com.rpframework.website.device.service.WidensService;
import com.rpframework.website.device.util.HConstants;

/**
 * 
 * @author 
 * @date 
 */
@Controller
@RequestMapping("/api2/opinion")
public class ApiOpinionAct extends AdminAct{
	 @Resource OpinionService opinionservice;
	 @Resource WidensService widenserivece;
	Gson gson =new Gson();
	/**
	 *
	 * @param pager
	 * @param model
	 * @param callback
	 * @param session
	 * @return
	 * @time 
	 */
	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String list(@RequestParam(value="pager", required=false)Pager<Opinion> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			@RequestParam (value="info",required=false) List<String> info,
			@RequestParam(value="text",required=false) String text,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Opinion>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager.getSearchMap().put("propertyid", String.valueOf(sessionuser.getEstateId()));
		pager.getSearchMap().put("typeid", String.valueOf(2));
		pager=opinionservice.getpager(pager);
		List<Opinion> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Opinion act : list) {
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			array.add(jsonObj);
		}
		String str = "";
		if(null!=info){
		for(String info1 :info){
			System.out.println(info1);
			if("1".equals(info1))str+="需求太少，";
			if("2".equals(info1))str+="品牌太少，";
			if("3".equals(info1))str+="软件功能，";
			if("4".equals(info1))str+="产品问题，";
			if("5".equals(info1))str+="互动问题，";
			if("5".equals(info1))str+="其他问题，";
			
		}
		}
		Opinion opinion  =  new Opinion();
				opinion.setInfo(str);
				opinion.setText(text);
				opinion.setCreateTime(System.currentTimeMillis()/1000);
				opinionservice.insert(opinion);
				if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
				return ""+json ;
	}
	@RequestMapping(value="/widens",produces = "application/json; charset=utf-8")
	public @ResponseBody String widens(@RequestParam(value="pager", required=false)Pager<Widens> pager, Map<Object, Object> model,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<Widens>();
		}
		User sessionuser=getSessionUser(session);
		if(sessionuser==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		pager=widenserivece.widenpager(pager);
		List<Widens> list=pager.getItemList();
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		JsonArray array=new JsonArray();
		json.add("arrays", array);
		for (Widens act : list) {
			JsonObject jsonObj = gson.toJsonTree(act).getAsJsonObject();
			array.add(jsonObj);
		}
			
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
	}

	
	
}
